const Graph = require("../template")

function testGraphPerformance() {
  console.log(
    "1. 添加顶点性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return graph.getVertexCount() === iterations && 
             duration < 1000 && // 应该在1秒内完成
             duration > 0
    })()
  )

  console.log(
    "2. 添加边性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 1000
      
      // 先添加顶点
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      const start = performance.now()
      
      // 添加边
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return graph.getEdgeCount() === iterations - 1 &&
             duration < 1000 &&
             duration > 0
    })()
  )

  console.log(
    "3. 删除顶点性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 500
      
      // 先添加顶点和边
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      // 删除一半顶点
      for (let i = 0; i < iterations / 2; i++) {
        graph.removeVertex(`V${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return graph.getVertexCount() === iterations / 2 &&
             duration < 1000 &&
             duration > 0
    })()
  )

  console.log(
    "4. 删除边性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 500
      
      // 先添加顶点和边
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      // 删除一半边
      for (let i = 0; i < iterations / 2; i++) {
        graph.removeEdge(`V${i}`, `V${i + 1}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return graph.getEdgeCount() === (iterations - 1) / 2 &&
             duration < 1000 &&
             duration > 0
    })()
  )

  console.log(
    "5. 邻接查询性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 1000
      
      // 先添加顶点和边
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      // 查询邻接顶点
      for (let i = 0; i < iterations; i++) {
        graph.getNeighbors(`V${i}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 500 && // 查询应该很快
             duration > 0
    })()
  )

  console.log(
    "6. 边存在性检查性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 1000
      
      // 先添加顶点和边
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      // 检查边是否存在
      for (let i = 0; i < iterations - 1; i++) {
        graph.hasEdge(`V${i}`, `V${i + 1}`)
      }
      
      const end = performance.now()
      const duration = end - start
      
      return duration < 500 && // 检查应该很快
             duration > 0
    })()
  )

  console.log(
    "7. DFS遍历性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 500
      
      // 构建大图
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      let count = 0
      graph.dfs('V0', () => count++)
      
      const end = performance.now()
      const duration = end - start
      
      return count === iterations &&
             duration < 1000 &&
             duration > 0
    })()
  )

  console.log(
    "8. BFS遍历性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 500
      
      // 构建大图
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      let count = 0
      graph.bfs('V0', () => count++)
      
      const end = performance.now()
      const duration = end - start
      
      return count === iterations &&
             duration < 1000 &&
             duration > 0
    })()
  )

  console.log(
    "9. 路径查找性能测试:",
    (() => {
      const graph = new Graph()
      const iterations = 200
      
      // 构建大图
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      
      for (let i = 0; i < iterations - 1; i++) {
        graph.addEdge(`V${i}`, `V${i + 1}`)
      }
      
      const start = performance.now()
      
      const path = graph.shortestPath('V0', `V${iterations - 1}`)
      
      const end = performance.now()
      const duration = end - start
      
      return path.length === iterations &&
             duration < 1000 &&
             duration > 0
    })()
  )

  console.log(
    "10. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(size) {
          const graph = new Graph()
          const start = performance.now()
          
          for (let i = 0; i < size; i++) {
            graph.addVertex(`V${i}`)
          }
          
          const end = performance.now()
          return (end - start) * this.multiplier
        }
      }
      
      const result = perfObj.processPerformance(500)
      return result > 0 && result < 1000
    })()
  )

  console.log(
    "11. 复杂对象this绑定性能测试:",
    (() => {
      const perfConfigObj = {
        config: { threshold: 1000 },
        processWithConfig: function(size) {
          const graph = new Graph()
          const start = performance.now()
          
          for (let i = 0; i < size; i++) {
            graph.addVertex(`V${i}`)
          }
          
          const end = performance.now()
          return (end - start) < this.config.threshold
        }
      }
      
      return perfConfigObj.processWithConfig(2000) === true
    })()
  )

  console.log(
    "12. 性能一致性测试:",
    (() => {
      const graph = new Graph()
      const iterations = 1000
      
      const start1 = performance.now()
      for (let i = 0; i < iterations; i++) {
        graph.addVertex(`V${i}`)
      }
      const end1 = performance.now()
      
      const start2 = performance.now()
      for (let i = 0; i < iterations; i++) {
        graph.getNeighbors(`V${i}`)
      }
      const end2 = performance.now()
      
      const addTime = end1 - start1
      const queryTime = end2 - start2
      
      return addTime > 0 &&
             queryTime > 0 &&
             queryTime < addTime && // 查询应该比添加快
             graph.getVertexCount() === iterations
    })()
  )
}

testGraphPerformance()
